package com.aaa.forum.services.impl;

import com.aaa.forum.common.AppResult;
import com.aaa.forum.common.ResultCode;
import com.aaa.forum.dao.SubscribeMapper;
import com.aaa.forum.exception.ApplicationException;
import com.aaa.forum.model.Subscribe;
import com.aaa.forum.model.User;
import com.aaa.forum.services.ISubscribeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class SubscribeServiceImpl implements ISubscribeService {

    @Resource
    private SubscribeMapper subscribeMapper;

    @Override
    public void like(Long userId, Long subscribedUserId) {
        if (userId == null || userId <= 0 || subscribedUserId == null || subscribedUserId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Subscribe subscribe = subscribeMapper.selectByTwoUserId(userId, subscribedUserId);
        int row;
        if (subscribe == null) {
            // 记录不存在，insert
            subscribe = new Subscribe();
            subscribe.setUserId(userId);
            subscribe.setSubscribedUserId(subscribedUserId);
            subscribe.setState((byte) 1);
            subscribe.setDeleteState((byte) 0);
            Date date = new Date();
            subscribe.setCreateTime(date);
            subscribe.setUpdateTime(date);
            row = subscribeMapper.insertSelective(subscribe);
        } else {
            // 记录存在，update
            Subscribe updateObject = new Subscribe();
            updateObject.setId(subscribe.getId());
            updateObject.setState((byte) 1);
            updateObject.setDeleteState((byte) 0);
            updateObject.setUpdateTime(new Date());
            row = subscribeMapper.updateByPrimaryKeySelective(updateObject);
        }
        if (row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public void cancel(Long userId, Long subscribedUserId) {
        if (userId == null || userId <= 0 || subscribedUserId == null || subscribedUserId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Subscribe subscribe = subscribeMapper.selectByTwoUserId(userId, subscribedUserId);
        if (subscribe == null || subscribe.getDeleteState() == 1 || subscribe.getState() == 0) return;
        Subscribe updateObject = new Subscribe();
        updateObject.setId(subscribe.getId());
        updateObject.setState((byte) 0);
        updateObject.setUpdateTime(new Date());
        int row = subscribeMapper.updateByPrimaryKeySelective(updateObject);
        if (row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public Byte getState(Long userId, Long subscribedUserId) {
        if (userId == null || userId <= 0 || subscribedUserId == null || subscribedUserId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return subscribeMapper.getState(userId, subscribedUserId);
    }

    @Override
    public Long getSubscribeCount(Long userId) {
        if (userId == null || userId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return subscribeMapper.getSubscribeCount(userId);
    }

    @Override
    public Long getFanCount(Long userId) {
        if (userId == null || userId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return subscribeMapper.getFanCount(userId);
    }

    @Override
    public List<User> getFanList(Long userId) {
        if (userId == null || userId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return subscribeMapper.getFanList(userId);
    }

    @Override
    public List<User> getSubscribeList(Long userId) {
        if (userId == null || userId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return subscribeMapper.getSubscribeList(userId);
    }
}
